package com.tianqu.tms.www.car.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsActionNotTimeout;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.Registry;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.TransferAcceptedInfoDto;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.www.car.controller.CarAutoTerminalSecondHandDetailController;
import com.tianqu.tms.www.car.viewhelper.CarAutoTerminalManageViewHelper;

/**
 * 特权车管理-初期显示
 * 
 * @author 
 *
 */
public class CarAutoTerminalSecondHandDetailAction extends TmsActionNotTimeout {
	
	private Logger logger = Logger.getLogger(CarAutoTerminalSecondHandDetailAction.class);
	private static final String BACKFLAG = "backFlag";
	private static final String TOSECONDHANDINPUT = "toSecondHandInput";
	
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		CarAutoTerminalManageViewHelper view = new CarAutoTerminalManageViewHelper();
		if (!StringUtil.isNullOrBlank(uc.getParameter("licensePlateLast"))
				&& !"1".equals(String.valueOf(uc.getAttribute(BACKFLAG)))) {
			CarAutoTerminalSecondHandDetailController controller  = new CarAutoTerminalSecondHandDetailController();
			controller.execute(uc);
			view = (CarAutoTerminalManageViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
			
			String defLicensePlate = Registry.getConfig(TmsConstants.TMS_DEFLICENSEPLATE_KEY);
			String licensePlateLast = uc.getParameter("licensePlateLast");
			String licensePlateCol = uc.getParameter("licensePlateCol");
			
			view.setDefLicensePlate(defLicensePlate);
			view.setLicensePlateLast(licensePlateLast);
			view.setLicensePlateCol(licensePlateCol);
			
			@SuppressWarnings("unchecked")
			List<TransferAcceptedInfoDto> list = view.getViewList();
			if(list == null || list.size() == 0){
				// 无数据状态
				view.setCheckStatus("0"); 
				request.setAttribute(VIEWHELP, view);
				return mapping.findForward(TOSECONDHANDINPUT);
			}
			
			TransferAcceptedInfoDto dto = (TransferAcceptedInfoDto)list.get(0);
			view.setSerial(dto.getSerial());
			view.setTransferPrice(dto.getTransferPrice());
//			view.setBackUrl(uc.getParameter("backUrl")+"?licensePlateLast=" + uc.getParameter("licensePlateLast") + "&licensePlateCol=" + uc.getParameter("licensePlateCol"));
//			view.setVinNum(uc.getParameter("vinNum"));
			
			// 1、如果符合条件数据的车辆过户状态（status）不为“1：查验照相、2：流程受理、18：退办继续受理”，那么弹出页面提示“此车辆已经受理，无法打印”
//			if (dto.getStatus() != 1
//					&& dto.getStatus() != 2
//					&& dto.getStatus() != 18) {
//				view.setCheckStatus(String.valueOf(dto.getStatus()));
//				request.setAttribute(VIEWHELP, view);
//				return mapping.findForward(TOSECONDHANDINPUT);
//			}

			view.setPhoto_1(dto.getPhoto_1());
			view.setLicensePlate(dto.getLicensePlate());
			view.setLicensePlateCol(dto.getLicensePlateCol());
			
//			view.setDefLicensePlate(uc.getParameter("defLicensePlate"));
//			view.setLicensePlateLast(uc.getParameter("licensePlateLast"));
//			view.setLicensePlateCol(uc.getParameter("licensePlateCol"));
			
			// 打印处理 start---
			String lcPrintBsFlag = uc.getParameter("lcPrintBsFlag");
			if (!StringUtil.isNullOrBlank(lcPrintBsFlag) 
					&& lcPrintBsFlag.equals("1")) {
//				TransferAcceptedInfo transferAcceptedInfo = view.getTransferAcceptedInfo();
				String uri = request.getRequestURI();
//				String contextPath = request.getContextPath();    // 应用的web目录的名称
				String url = request.getRequestURL().toString();
				url = url.replace(uri, "");// + contextPath;
				view.setLcText(this.getPrintText(dto, url));	
			}
			view.setLcPrintBsFlag(lcPrintBsFlag);
			// 打印处理 End---
			
			uc.removeAttribute(BACKFLAG);
		}
		request.setAttribute(VIEWHELP, view);	
		
		logger.debug(this.getClass().getName() + "#doAction was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}
	
	/**
	 * 获取打印内容
	 * 
	 * @param controller
	 * @return
	 */
	private String getPrintText(TransferAcceptedInfoDto dto, String url) {
		Codes code = null;
		try {
			code = Codes.getInstance();
		} catch (TmsDaoException e) {
		}
		String lvin = "";
		String registration = "";
		String transferRegister = "";
		String changesVacate = "";
		String vinNum = "√";
		String engineNum = "——";
		String brandModel = "——";
		String drivingRecord = "——";
		String plateOrTireExterior = "——";
		String tireIntact = "——";
		String seatBelt = "——";
		String numberAxes = "——";
		String tireSize = "——";
		String lateralPortionOf = "——";
		String reflectiveMark = "——";
		String fireExtinguisher = "——";
		String security = "——";
		String externalMarking = "——";
		String siren = "——";
		String securityTechnology = "——";

//		if (dto.getRegistration() == 1) {// 注册登记
//			registration = "√";
//		}
//		if (dto.getTransferRegister() == 1) {// 转移登记
//			transferRegister = "√";
//		}
//		if (dto.getChangesVacate() == 1) {// 变更迁出
//			changesVacate = "√";
//		}
		if ("1".equals(dto.getVinNum())) {// 车辆识别代号-----------------------------
			vinNum = "√";
		}
		if (dto.getVinNum() == null|| "".equals(dto.getVinNum())) {
			vinNum = "";
		}
		if ("1".equals(dto.getEngineNum())) {// 发动机型号/代码
			engineNum = "√";
		}
		if (dto.getEngineNum() == null|| "".equals(dto.getEngineNum())) {
			engineNum = "";
		}
		if ("1".equals(dto.getBrandModel())) {// 车辆品牌/型号
			brandModel = "√";
		}
		if (dto.getBrandModel() == null|| "".equals(dto.getBrandModel())) {
			brandModel = "";
		}
		if ("1".equals(dto.getPlateOrTireExterior())) {// 号牌/车辆外观形状
			plateOrTireExterior = "√";
		}
		if (dto.getPlateOrTireExterior() == null|| "".equals(dto.getPlateOrTireExterior())) {
			plateOrTireExterior = "";
		}
		if ("1".equals(dto.getTireIntact())) {// 轮胎完好情况
			tireIntact = "√";
		}
		if (dto.getTireIntact() == null|| "".equals(dto.getTireIntact())) {
			tireIntact = "";
		}
		if ("1".equals(dto.getSeatBelt())) {// 安全带、三角警告牌
			seatBelt = "√";
		}
		if (dto.getSeatBelt() == null|| "".equals(dto.getSeatBelt())) {
			seatBelt = "";
		}
		if ("1".equals(dto.getNumberAxes())) {// 外廓尺寸、轴数
			numberAxes = "√";
		}
		if (dto.getNumberAxes() == null
				|| "".equals(dto.getNumberAxes())) {
			numberAxes = "";
		}
		if ("1".equals(dto.getTireSize())) {// 轮胎规格
			tireSize = "√";
		}
		if (dto.getTireSize() == null
				|| "".equals(dto.getTireSize())) {
			tireSize = "";
		}
		if ("1".equals(dto.getLateralPortionOf())) {// 侧后部防护装置
			lateralPortionOf = "√";
		}
		if (dto.getLateralPortionOf() == null
				|| "".equals(dto.getLateralPortionOf())) {
			lateralPortionOf = "";
		}
		if ("1".equals(dto.getReflectiveMark())) {// 车身反光标示
			reflectiveMark = "√";
		}
		if (dto.getReflectiveMark() == null
				|| "".equals(dto.getReflectiveMark())) {
			reflectiveMark = "";
		}
		if ("1".equals(dto.getFireExtinguisher())) {// 灭火器
			fireExtinguisher = "√";
		}
		if (dto.getFireExtinguisher() == null
				|| "".equals(dto.getFireExtinguisher())) {
			fireExtinguisher = "";
		}
		if ("1".equals(dto.getDrivingRecord())) {// 行驶记录装置
			drivingRecord = "√";
		}
		if (dto.getDrivingRecord() == null
				|| "".equals(dto.getDrivingRecord())) {
			drivingRecord = "";
		}
		if ("1".equals(dto.getSecurity())) {// 安全出口、安全锤
			security = "√";
		}
		if (dto.getSecurity() == null
				|| "".equals(dto.getSecurity())) {
			security = "";
		}
		if ("1".equals(dto.getExternalMarking())) {// 外部标示、文字
			externalMarking = "√";
		}
		if (dto.getExternalMarking() == null
				|| "".equals(dto.getExternalMarking())) {
			externalMarking = "";
		}
		if ("1".equals(dto.getSiren())) {// 标志灯具、警报器
			siren = "√";
		}
		if (dto.getSiren() == null
				|| "".equals(dto.getSiren())) {
			siren = "";
		}
		if ("1".equals(dto.getSecurityTechnology())) {// 安全技术检验合格证明
			securityTechnology = "√";
		}
		if (dto.getSecurityTechnology() == null
				|| "".equals(dto.getSecurityTechnology())) {
			securityTechnology = "";
		}
		String strVin = "";
		if (StringUtil.isNullOrBlank(dto.getLicensePlate())) {
			lvin = dto.getVin();
			strVin = "<div class='jp-label jp-comp-13'>";
		} else {
			lvin = dto.getLicensePlate();
			strVin = "<div class='jp-label jp-comp-28'>";
		}
		StringBuffer str = new StringBuffer();

		String color = dto.getCy_color();
		if (color == null || "".equals(color)) {
			color = "";
		}
		String cy_passenger = dto.getCy_passenger();
		if (cy_passenger == null || "".equals(cy_passenger)) {
			cy_passenger = "";
		}
		String cy_carType = dto.getCarType();
		if (cy_carType == null || "".equals(cy_carType)) {
			cy_carType = "";
		}
		String checkConclusion = dto.getCheckConclusion();
		if (checkConclusion == null || "".equals(checkConclusion)) {
			checkConclusion = "";
		}
		String pp = "";
		if(code != null && dto.getCardType()!=null && !"".equals(dto.getCardType()))
		{
			pp = code.getItemValueByItemIdAndItemCode(Codes.PLATE_TYPE,dto.getCardType());
		}
		str.append(""
						+ "<img class='jp-paper-background' src='style/images/memuC.jpg' style='WIDTH: 766px; HEIGHT: 1083px'/>"
						+ "<div class='jp-label jp-comp-1'>"
						+ registration
						+ "</div>"
						+ "<div class='jp-label jp-comp-2'>"
						+ transferRegister
						+ "</div>"
						+ "<div class='jp-label jp-comp-3'>"
						+ changesVacate
						+ "</div>"
						+ "<div class='jp-label jp-comp-4'>"
						+ vinNum
						+ "</div>"
						+ "<div class='jp-label jp-comp-5'>"
						+ engineNum
						+ "</div>"
						+ "<div class='jp-label jp-comp-6'>"
						+ brandModel
						+ "</div>"
						+ "<div class='jp-label jp-comp-7'>"
						+ drivingRecord
						+ "</div>"
						+ "<div class='jp-label jp-comp-8'>"
						+ color
						+ "</div>"
						+ "<div class='jp-label jp-comp-9'>"
						+ cy_passenger
						+ "</div>"
						+ "<div class='jp-label jp-comp-10'>"
						+ cy_carType
						+ "</div>"
						+ "<div class='jp-label jp-comp-11'>"
						+ plateOrTireExterior
						+ "</div>"
						+ "<div class='jp-label jp-comp-12'>"
						+ checkConclusion
						+ "</div>"
						+ strVin
						+ lvin
						+ "</div>"
						+ "<div class='jp-label jp-comp-14'>"
						+ dto.getSerial()
						+ "</div>"
						+ "<div class='jp-label jp-comp-15'>"
						+ pp
						+ "</div>"
						+ "<div class='jp-image jp-comp-16'>"
						+ "<img class=jp-image-view src='"
						+  url + dto.getPhoto_1()
						+ "' style='WIDTH: 99mm; HEIGHT: 65mm'>"
						+ "</div>"
						+ "<div class='jp-label jp-comp-17'>"
						+ tireIntact
						+ "</div>"
						+ "<div class='jp-label jp-comp-18'>"
						+ seatBelt
						+ "</div>"
						+ "<div class='jp-label jp-comp-19'>"
						+ numberAxes
						+ "</div>"
						+ "<div class='jp-label jp-comp-20'>"
						+ tireSize
						+ "</div>"
						+ "<div class='jp-label jp-comp-21'>"
						+ lateralPortionOf
						+ "</div>"
						+ "<div class='jp-label jp-comp-22'>"
						+ reflectiveMark
						+ "</div>"
						+ "<div class='jp-label jp-comp-23'>"
						+ fireExtinguisher
						+ "</div>"
						+ "<div class='jp-label jp-comp-24'>"
						+ security
						+ "</div>"
						+ "<div class='jp-label jp-comp-25'>"
						+ externalMarking
						+ "</div>"
						+ "<div class='jp-label jp-comp-26'>"
						+ siren
						+ "</div>"
						+ "<div class='jp-label jp-comp-27'>"
						+ securityTechnology + "</div>" +
						"<div class='jp-label jp-comp-29'><img src='style/images/guohui.jpg' width='30mm' height='30mm'>" +
						"</div>" +
						"<div class='jp-label jp-comp-30'>大连市公安局交警支队车管所旧车交易市场机动车登记服务站" +
						"</div>" +
						"<div class='jp-label jp-comp-31'><img src='style/images/vin_before.png' width='95mm' height='19mm'>" +
						"</div>" +
						"<div class='jp-label jp-comp-32'><img src='style/images/vin_after.png' width='170mm' height='20mm'></div>");

		return str.toString();
	}
}
